package com.example.maze.model;

import java.util.Objects;

/**
 * 坐标类，表示迷宫中的一个位置
 * 
 * @author maze-demo
 */
public class Coordinate {
    private final int row;
    private final int col;

    public Coordinate(int row, int col) {
        this.row = row;
        this.col = col;
    }

    public int getRow() {
        return row;
    }

    public int getCol() {
        return col;
    }

    /**
     * 获取相邻的坐标（上下左右）
     */
    public Coordinate[] getNeighbors() {
        return new Coordinate[]{
            new Coordinate(row - 1, col), // 上
            new Coordinate(row + 1, col), // 下
            new Coordinate(row, col - 1), // 左
            new Coordinate(row, col + 1)  // 右
        };
    }

    /**
     * 计算曼哈顿距离（用于A*算法）
     */
    public int manhattanDistance(Coordinate other) {
        return Math.abs(this.row - other.row) + Math.abs(this.col - other.col);
    }

    /**
     * 检查坐标是否在指定范围内
     */
    public boolean isValid(int maxRow, int maxCol) {
        return row >= 0 && row < maxRow && col >= 0 && col < maxCol;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Coordinate that = (Coordinate) obj;
        return row == that.row && col == that.col;
    }

    @Override
    public int hashCode() {
        return Objects.hash(row, col);
    }

    @Override
    public String toString() {
        return String.format("(%d,%d)", row, col);
    }
}